<?php
class transpetty_model extends CI_Model{
	private $Kdtransaksi;
	private $Kodepettycash;
	private $Tanggal;
	private $Keterangan;
	private $Amount;
    private $Createdby;
    private $Createdon;
    private $Lastupdateby;
  	private $Lastupdateon;
  	private $Active;
	
	function setKdtransaksi($kdtrans){
		$this->Kdtransaksi = $kdtrans;
	}
	function getKdtransaksi(){
		return $this->Kdtransaksi;
	}
	
	function setKodepettycash($kdpetty){
		$this->Kodepettycash = $kdpetty;
	}
	function getKodepettycash(){
		return $this->Kodepettycash;
	}
	
	function setTanggal(){
		$dateNow = date('Y-m-d');
		$this->Tanggal = $dateNow;
	}
	function getTanggal(){
		return $this->Tanggal;
	}

	function setKeterangan($ket){
		$this->Keterangan = $ket;
	}
	function getKeterangan(){
		return $this->Keterangan;
	}
	
	function setAmount($amount){
		$this->Amount = $amount;
	}
	function getAmount(){
		return $this->Amount;
	}
	
	function setCreatedBy($c_by){
		$this->CreatedBy = $c_by;
	}
	function getCreatedBy(){
		return $this->CreatedBy;
	}

	function setCreatedOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->Createdon = $dateNow;
	}
	function getCreatedOn(){
		return $this->Createdon;
	}

	function setLastUpdateBy($u_by){
		$this->Lastupdateby = $u_by;
	}
	function getLastUpdateBy(){
		return $this->Lastupdateby;
	}
	
	function setLastUpdateOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->Lastupdateon = $dateNow;
	}
	function getLastUpdateOn(){
		return $this->Lastupdateon;
	}

	function setActive($active_val){
		$this->Active = $active_val;
	}
	function getActive(){
		return $this->Active;
	}
	// Call the module constructor
	function __construct(){
		parent::__construct();
	}
	
	function gettransCode()
	{
		$query = $this->db->query("select MAX(RIGHT(Kdtransaksi,4)) as kdtrans from transaksi_petty_cash");
		$kd = "";
		
		if($query-> num_rows() > 0)
		{
			foreach($query->result() as $idtrans)
			{
				$tmptrans = ((int)$idtrans->kdtrans)+1;
				$kd = sprintf("%04s", $tmptrans);
			}
			
		}
		else
		{
			$kd = "0001";
		}
		
		$kd = "TP".$kd; 
		
		return $kd;
	}
	
	function insert(){
		/*$query = $this->db->query("select max(Kodepettycash) kbank from transaksi_petty_cash");
		$res = $query->result();
		$this->setKodepettycash(($res[0]->kbank) + 1);*/
		
		$query = $this->db->query("select MAX(RIGHT(Kdtransaksi,4)) as kdtrans from transaksi_petty_cash");
		$kd = "";
		
		if($query-> num_rows() > 0)
		{
			foreach($query->result() as $idtrans)
			{
				$tmptrans = ((int)$idtrans->kdtrans)+1;
				$kd = sprintf("%04s", $tmptrans);
			}
			
		}
		else
		{
			$kd = "0001";
		}
			
		$kdtransok="TP".$kd;
		$this->setKdtransaksi($kdtransok);
		
		$data = array(
			'Kdtransaksi' => $this->getKdtransaksi(),
			'Kodepettycash' => $this->getKodepettycash(),
			'Tanggal' => $this->getTanggal(),
			'Keterangan' => $this->getKeterangan(),
			'Amount' => $this->getAmount(),
			'CreatedBy' => $this->getCreatedBy(),
			'CreatedOn' => $this->getCreatedOn(),
			'LastUpdateBy' => 'null',
			'Lastupdateon' => 'null',
			'Active' => '1'					
		);
		$this->db->insert('transaksi_petty_cash',$data);
	}
	
	function ubahtranspetty() 
	{
            $data = array(
                'Keterangan' => $this->getKeterangan(),
				'Amount' => $this->getAmount(),
                'LastUpdateBy' => $this->getLastUpdateBy(),
                'LastUpdateOn' => $this->getLastUpdateOn()
            );
 
        $this->db->where(array('Kdtransaksi' => $this->getKdtransaksi()));
        $this->db->update('transaksi_petty_cash', $data);
    }

    function deletetranspetty() {
        $data = array(
            'Active' => '0'
        );
        $this->db->where(array('Kdtransaksi' => $this->getKdtransaksi()));
        $this->db->update('transaksi_petty_cash', $data);
    }
	
	function getList(){
		$this->db->select("transaksi_petty_cash.Kdtransaksi,transaksi_petty_cash.Tanggal,transaksi_petty_cash.Kodepettycash,master_petty_cash.namapettycash,transaksi_petty_cash.Keterangan,transaksi_petty_cash.Amount")->join('master_petty_cash', 'master_petty_cash.Kodepettycash=transaksi_petty_cash.Kodepettycash')
		->where(array('transaksi_petty_cash.Active'=>'1'));
		$query = $this->db->get('transaksi_petty_cash');
		return $query->result();
	}
	
	function countGetList(){
		$this->db->select("transaksi_petty_cash.Kdtransaksi,transaksi_petty_cash.Tanggal,transaksi_petty_cash.Kodepettycash,master_petty_cash.namapettycash,transaksi_petty_cash.Keterangan,transaksi_petty_cash.Amount")->join('master_petty_cash', 'master_petty_cash.Kodepettycash=transaksi_petty_cash.Kodepettycash')
		->where(array('transaksi_petty_cash.Active'=>'1'));
		$query = $this->db->get('transaksi_petty_cash');
		return $query->num_rows();
	}
	
	function getListlimit($limit,$start){
		$this->db->select("transaksi_petty_cash.Kdtransaksi,transaksi_petty_cash.Tanggal,transaksi_petty_cash.Kodepettycash,master_petty_cash.namapettycash,transaksi_petty_cash.Keterangan,transaksi_petty_cash.Amount")->join('master_petty_cash', 'master_petty_cash.Kodepettycash=transaksi_petty_cash.Kodepettycash')
		->where(array('transaksi_petty_cash.Active'=>'1'))
		->limit($limit,$start);
		$query = $this->db->get('transaksi_petty_cash');
		return $query->result();
	}
	
	function getkdtrans(){
		$this->db->select("transaksi_petty_cash.Kdtransaksi,transaksi_petty_cash.Tanggal,transaksi_petty_cash.Kodepettycash,master_petty_cash.namapettycash,transaksi_petty_cash.Keterangan,transaksi_petty_cash.Amount")->join('master_petty_cash', 'master_petty_cash.Kodepettycash=transaksi_petty_cash.Kodepettycash')
		->where(array('transaksi_petty_cash.Kdtransaksi' => $this->getKdtransaksi(),'transaksi_petty_cash.Active'=>'1'));
		$query = $this->db->get('transaksi_petty_cash');
		return $query->result();
	}
	
	function getkdtranslike(){
		$this->db->select("transaksi_petty_cash.Kdtransaksi,transaksi_petty_cash.Tanggal,transaksi_petty_cash.Kodepettycash,master_petty_cash.namapettycash,transaksi_petty_cash.Keterangan,transaksi_petty_cash.Amount")->join('master_petty_cash', 'master_petty_cash.Kodepettycash=transaksi_petty_cash.Kodepettycash')
		->where(array('transaksi_petty_cash.Active'=>'1'))->like(array('transaksi_petty_cash.Kdtransaksi' => $this->getKdtransaksi()));
		$query = $this->db->get('transaksi_petty_cash');
		return $query->result();
	}
	
	function getkdpettylike(){
		$this->db->select("transaksi_petty_cash.Kdtransaksi,transaksi_petty_cash.Tanggal,transaksi_petty_cash.Kodepettycash,master_petty_cash.namapettycash,transaksi_petty_cash.Keterangan,transaksi_petty_cash.Amount")->join('master_petty_cash', 'master_petty_cash.Kodepettycash=transaksi_petty_cash.Kodepettycash')
		->where(array('transaksi_petty_cash.Active'=>'1'))->like(array('transaksi_petty_cash.Kodepettycash' => $this->getKodepettycash()));
		$query = $this->db->get('transaksi_petty_cash');
		return $query->result();
	}
}